#!/usr/bin/bash


rm inone/include -rvf

destdir=inone/include/ttds

mkdir $destdir -p

header=$destdir/ttds.h

function condition_file(){
    cat <<- EOF >>$header
#ifdef $1
EOF
    cat $2 | sed -e '/^#include "ttds.*/d' >>$header
    cat <<- EOF >>$header
#endif /* $1 */
EOF
}

cat <<- EOF >$header
/*
 * File: ttds.h
 * Author: Liang YaoZhan <262666882@qq.com>
 * Brief:  this file is auto gennerated.
 *
 * Copyright (c) 2022 - 2023 Liang YaoZhan <262666882@qq.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2023-08-05 Liang YaoZhan <262666882@qq.com> created
 *
 */

#ifndef ttds_H
#define ttds_H

#include "ttds/ttds_config.h"
EOF

includedir=../include/ttds
srcdir=../src

if [ ! -d $includedir ];  then 
    echo "dir $includedir not found"
    exit 3
fi

cat $includedir/ttds_queue.h | sed -e '/^#include "ttds.*/d' >>$header
cat $includedir/ttds_timer.h | sed -e '/^#include "ttds.*/d' >>$header
condition_file TTDS_USING_SWITCHCASE_THREAD $includedir/ttds_core.h
condition_file TTDS_USING_SWITCHCASE_THREAD $includedir/ttds_message_queue.h
condition_file TTDS_USING_CJOBQ $includedir/ttds_cjobq.h
condition_file TTDS_USING_COTHREAD_POOL $includedir/ttds_cotask.h
condition_file TTDS_USING_COTHREAD_POOL $includedir/ttds_interrupt_service.h

cat <<- EOF >>$header
#endif /* header file once */


#ifdef DEF_MYPT_C_FUNCTION_BODY
#ifdef __cplusplus
extern "C"
{
#endif
EOF

condition_file TTDS_USING_CJOBQ $srcdir/ttds_cjobq.c
condition_file TTDS_USING_SWITCHCASE_THREAD $srcdir/ttds_core.c
condition_file TTDS_USING_SWITCHCASE_THREAD $srcdir/ttds_message_queue.c
condition_file TTDS_USING_COTHREAD_POOL $srcdir/ttds_cotask.c
condition_file TTDS_USING_COTHREAD_POOL $srcdir/ttds_interrupt_service.c
cat $srcdir/ttds_timer.c | sed -e '/^#include "ttds.*/d' >>$header

cat <<- EOF >>$header

#ifdef __cplusplus
}
#endif
#endif /* source file def: DEF_MYPT_C_FUNCTION_BODY */

#ifdef __cplusplus
EOF

cat $includedir/ttds.hpp | sed -e '/^#include "ttds.*/d' >>$header
cat <<- EOF >>$header
#endif
EOF

sed -i 's/ttds_thread_sys_tick_get_ms/ttds_cothread_sys_tick_get_ms/g' $header

#mkdir -p ../../allinone_example/include/ttds/
#mv $header ../../allinone_example/include/ttds/
#cp -v $includedir/ttds_config.h $destdir/
cp $header /e/code/working/github/ttds_example/include/ttds
mv $header /f/lj/bootloader/bl/Inc/ttds

########################################################


